package com.ibiaoke.customer.service;


import com.ibiaoke.common.configs.AliPayConfigs;
import com.ibiaoke.common.configs.AppAliPayConfigs;
import com.ibiaoke.common.configs.GlobalConfigs;
import com.ibiaoke.common.configs.GlobalConfigsKey;
import com.ibiaoke.common.consts.BusinessDef;
import com.ibiaoke.common.utils.AssertUtils;
import com.ibiaoke.common.utils.CashUtil;
import com.ibiaoke.customer.consts.TradeStatusConsts;
import com.ibiaoke.customer.entity.TradeRecorder;
import com.ibiaoke.pay.AppAliPay;
import com.ibiaoke.pay.AppWeChatPayClient;
import com.ibiaoke.pay.BkAliPayClient;
import com.ibiaoke.pay.BkWeChatPayClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Hashtable;
import java.util.Map;

/**
 * 余额充值
 */
@Service
public class DepositChargeServiceImpl implements DepositChargeService {

    @Autowired
    private BkWeChatPayClient bkWeChatPayClient;
    @Autowired
    private BkAliPayClient bkAliPayClient;

    @Autowired
    private AppAliPay appAliPay;
    @Autowired
    private AppWeChatPayClient appWeChatPayClient;

    @Override
    public Map<String, String> chargeDepositByWeChat(TradeRecorder tradeRecorder, String openId) {
        AssertUtils.notNull(tradeRecorder,"请选择订单");
        validate(tradeRecorder);
        String body ="让我来果源充值："+ tradeRecorder.getTradeNo();
        String out_trade_no = tradeRecorder.getTradeNo();
        String total_fee = String.valueOf(CashUtil.getCent(tradeRecorder.getMonney()));
        String trade_type = "JSAPI";
        String notify_url = GlobalConfigs.getConfigure(GlobalConfigsKey.WeChat.NOTIFY_URL);
        notify_url=notify_url.replace("BUSINESS_TYPE", BusinessDef.PAY_DEPOSIT);
        return bkWeChatPayClient.wxPay(openId, body, out_trade_no, total_fee, trade_type, notify_url);
    }

    @Override
    public String chargeDepositByAliPay(TradeRecorder tradeRecorder) {
        AssertUtils.notNull(tradeRecorder,"请选择订单");
        validate(tradeRecorder);
        //商户订单号
        String out_trade_no = tradeRecorder.getTradeNo();
        String subject = "让我来果源订单:"+out_trade_no;
        String total_amount = tradeRecorder.getMonney().toString();
        String notify_url= AliPayConfigs.getNotifyURL();
        notify_url=notify_url.replace("BUSINESS_TYPE", BusinessDef.PAY_DEPOSIT);
        String return_url = AliPayConfigs.getReturnURL();
        return bkAliPayClient.aliPay(out_trade_no, subject, total_amount,notify_url,return_url);
    }

    @Override
    public String chargeDepositByAliAppPay(TradeRecorder tradeRecorder) {
        AssertUtils.notNull(tradeRecorder,"请选择订单");
        validate(tradeRecorder);
        //商户订单号
        String out_trade_no = tradeRecorder.getTradeNo();
        String total_amount = tradeRecorder.getMonney().toString();
        Map<String, Object> body = new Hashtable<String, Object>();
        body.put("out_trade_no", out_trade_no);
        body.put("subject", "让我来果源订单:" + out_trade_no);
        body.put("total_amount", total_amount);
        body.put("notify_url", AppAliPayConfigs.getNotifyURL().replace("BUSINESS_TYPE", BusinessDef.PAY_DEPOSIT).toString());
        body.put("return_url",  AppAliPayConfigs.getReturnURL());
        return appAliPay.aliPay(total_amount,body);
    }

    @Override
    public Map<String, Object> chargeDepositByWeChatApp(TradeRecorder tradeRecorder, String ip) {
        AssertUtils.notNull(tradeRecorder,"请选择订单");
        validate(tradeRecorder);
        String body ="让我来果源充值："+ tradeRecorder.getTradeNo();
        String out_trade_no = tradeRecorder.getTradeNo();
        String total_fee = String.valueOf(CashUtil.getCent(tradeRecorder.getMonney()));
        String trade_type = "APP";
        String notify_url = GlobalConfigs.getConfigure(GlobalConfigsKey.WeChat.NOTIFY_URL);
        notify_url=notify_url.replace("BUSINESS_TYPE", BusinessDef.WX_PAY_DEPOSIT);
        return appWeChatPayClient.appWxPay( body, out_trade_no, total_fee, trade_type, notify_url,ip);
    }

    public void validate(TradeRecorder tradeRecorder) {
        AssertUtils.notNull(tradeRecorder, "请选择支付订单");
        AssertUtils.requireEquals(tradeRecorder.getStatus(), TradeStatusConsts.DEALING, "订单已支付");
        AssertUtils.requireTrue(tradeRecorder.getMonney().compareTo(new BigDecimal("0.01"))>=0, "支付金额不能小于0.01元");
    }
}
